package com.hiwan.unions.activity.system.newfragment.presenter;

import android.app.Activity;

import com.hiwan.unions.activity.base.mvpbase.Presenter;
import com.hiwan.unions.activity.system.newfragment.Contract.NewsListContract;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.JsonUtil;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by llg on 2017/9/25.
 */
public class NewsListPresenter extends Presenter<NewsListContract.View> implements NewsListContract.Presenter, HttpListener<JSONObject> {

    @Override
    public void getOrderData(Activity activity, int content, boolean dialog) {
        //消息列表
        Request<JSONObject> request = NoHttp.createJsonObjectRequest(ConstsNet.message_list);
        request.add("pageNumber", content);
        request.add("pageSize", 10);
        CallServer.getRequestInstance().add(activity, 0, request, this, dialog);
//        mView.showErrorMsg("您还没有收到消息", false);

    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        mView.loadSuccess();
        JSONObject jsonObject = response.get();
        jsonObject = JsonUtil.getJSONObject(jsonObject, "data");
        switch (what) {
            case 0:
                JSONArray jsonArray = JsonUtil.getJSONArray(jsonObject, "list");
                if (jsonArray.length() > 0) {
                    mView.onOrderList(jsonArray, JsonUtil.getBoolean(jsonObject, "lastPage"));
                } else {
                    mView.showErrorMsg("您还没有收到消息", false);
                }
                break;
        }
    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {
        mView.showErrorMsg(networkMillis, true);
    }
}
